package com.example.demo.controller;

import com.example.demo.pojo.Account;
import com.example.demo.pojo.Competition;
import com.example.demo.pojo.StuCom;
import com.example.demo.pojo.User;
import com.example.demo.pojo.vo.Count;
import com.example.demo.result.Result;
import com.example.demo.service.StuComService;
import com.example.demo.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/stucom")
public class StuComController {
    @Autowired
    private StuComService stuComService;
    @Autowired
    private JwtUtil jwtUtil;//用于解析token
    @Value("${location}")
    private String location;

    //展示比赛列表
    @PostMapping("listCom/{current}/{limit}")
    public Result getComList(@PathVariable Integer current, @PathVariable Integer limit,
                             @RequestHeader("X-Token") String token,
                             @RequestBody Competition competition){
        PageHelper.startPage(current,limit);
        PageInfo<StuCom> comPageInfo = stuComService.listCom(token,competition);
        return Result.success(comPageInfo);
    }

    //学生报名参赛功能
    @PostMapping("addStuCom")
    public Result addStuCom(@RequestBody Competition competition,@RequestHeader("X-Token") String token){
        Integer comId = competition.getId();
        Account currentUser = jwtUtil.parseToken(token, Account.class);
        Integer userId = currentUser.getId();
        User user=new User();
        Competition competition1=new Competition();
        StuCom stuCom=new StuCom();
        user.setId(userId);
        competition1.setId(comId);
        stuCom.setUser(user);
        stuCom.setCompetition(competition1);
        int i=stuComService.addStuCom(stuCom);
        if(i>0){
            return Result.success();
        }else{
            return Result.fail();
        }
    }

    //展示参加某比赛的学生
    @PostMapping("listStudent/{current}/{limit}/{id}")
    public Result listStudent(@PathVariable Integer current, @PathVariable Integer limit,
                              @PathVariable Integer id,@RequestBody User user){
        PageHelper.startPage(current,limit);
        String username = user.getUsername();
        PageInfo<StuCom> stuPageInfo = stuComService.listStudent(id,username);
        return Result.success(stuPageInfo);
    }

    //上传作品
    @PostMapping("upload/{id}/{token}")
    public Result upload(@PathVariable String token,
                         @PathVariable Integer id, MultipartFile file){
        StuCom stuCom=transferFile(id,token,file);
        int i=stuComService.upload(stuCom);
        if(i>0){
            return Result.success();
        }else{
            return Result.fail();
        }
    }

    private StuCom transferFile(Integer id, String token, MultipartFile file){
        Account account = jwtUtil.parseToken(token, Account.class);
        Integer userId = account.getId();
        User user=new User();
        Competition competition=new Competition();
        StuCom stuCom=new StuCom();
        user.setId(userId);
        competition.setId(id);
        stuCom.setCompetition(competition);
        stuCom.setUser(user);

        String originalFilename = file.getOriginalFilename();
        int index=originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(index);
        String prefix=System.nanoTime()+"";
        String path=prefix+suffix;

        File file1 = new File(location);
        if(!file1.exists()){
            file1.mkdirs();
        }
        File file2 = new File(file1,path);
        try {
            file.transferTo(file2);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        stuCom.setImage(path);
        return stuCom;
    }

    //颁奖
    @PostMapping("givePrize/{studentId}/{competitionId}")
    public Result givePrize(@PathVariable Integer studentId,
                            @PathVariable Integer competitionId){

        int i=stuComService.givePrize(studentId,competitionId);
        if(i>0){
            return Result.success();
        }else{
            return Result.fail();
        }
    }
}
